package com.blue.prepose.controller;

import com.alibaba.fastjson.JSON;
import com.blue.gateway.common.request.PayRequest;
import com.blue.gateway.common.response.PayResponse;
import com.blue.gateway.config.UrlConfig;
import com.blue.gateway.dto.Result;
import com.blue.gateway.enums.PayChannelEnum;
import com.blue.prepose.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaoyiming
 * @date 2020/5/3 22:33
 * @depiction
 */
@Slf4j
@Controller
public class PayController {

    @Autowired
    private PayService payService;
    @Autowired
    private UrlConfig urlConfig;

    @RequestMapping("/payalipc")
    public ModelAndView payalipc(String no) {
        PayRequest payRequest = new PayRequest();
        payRequest.setPayChannelEnum(PayChannelEnum.ALIPAY_PC);
        payRequest.setNotifyUrl(urlConfig.getNotifyAli());
        payRequest.setReturnUrl(urlConfig.getReturnUrl());

        payRequest.setOutTradeNo(no);
        payRequest.setTotalAmount(new BigDecimal("0.02"));
        payRequest.setSubject("测试一下");
        Result<PayResponse> result = payService.pay(payRequest);


        Map<String, String> map = new HashMap<>();
        map.put("body", JSON.toJSONString(result));
        map.put("url", result.getData().getPayUrl());
        return new ModelAndView("alipay", map);
    }
    @RequestMapping("/payalih5")
    public ModelAndView payalih5(String no) {
        PayRequest payRequest = new PayRequest();
        payRequest.setPayChannelEnum(PayChannelEnum.ALIPAY_H5);
        payRequest.setNotifyUrl(urlConfig.getNotifyAli());
        payRequest.setReturnUrl(urlConfig.getReturnUrl());

        payRequest.setOutTradeNo(no);
        payRequest.setTotalAmount(new BigDecimal("0.02"));
        payRequest.setSubject("测试一下");
        Result<PayResponse> result = payService.pay(payRequest);


        Map<String, String> map = new HashMap<>();
        map.put("body", JSON.toJSONString(result));
        map.put("url", result.getData().getPayUrl());
        return new ModelAndView("alipay", map);
    }
    //http://127.0.0.1/paywxh5?no=7897122313
    //但是我跳转了之后还是出现同样的问题，是因为权限的问题，商户平台申请h5支付时会填写产品域名，
    //请求后端支付接口时必须与产品域名一样，这样跳转后是在产品域名下请求mwebUrl,
    //如果不是在这个指定的产品域名下还是会出现“商家参数格式有误，请联系商家解决”的问题。
    @RequestMapping("/paywxh5")
    public String paywxh5(String no) throws UnsupportedEncodingException {
        PayRequest payRequest = new PayRequest();
        payRequest.setPayChannelEnum(PayChannelEnum.WX_H5);
        payRequest.setNotifyUrl(urlConfig.getNotifyWx());
        payRequest.setReturnUrl(urlConfig.getReturnUrl());

        payRequest.setOutTradeNo(no);
        payRequest.setTotalAmount(new BigDecimal("0.02"));
        payRequest.setSubject("测试一下");

        Result<PayResponse> result = payService.pay(payRequest);
        return "redirect:" + result.getData().getPayUrl() + "&redirect_url=" + URLEncoder.encode(urlConfig.getReturnUrl(), "UTF-8");
    }

    //http://127.0.0.1/paywxqr?no=7897122313
    @RequestMapping("/paywxqr")
    public ModelAndView wxpayqr(String no) {
        PayRequest payRequest = new PayRequest();
        payRequest.setPayChannelEnum(PayChannelEnum.WX_QR);
        payRequest.setNotifyUrl(urlConfig.getNotifyWx());
        payRequest.setReturnUrl(urlConfig.getReturnUrl());

        payRequest.setOutTradeNo(no);
        payRequest.setTotalAmount(new BigDecimal("0.02"));
        payRequest.setSubject("测试一下");

        Result<PayResponse> result = payService.pay(payRequest);

        Map<String, String> map = new HashMap<>();
        if (result.getData().getPayUrl() != null) {
            map.put("body", result.getData().getPayUrl());
            map.put("url", result.getData().getPayUrl());
        } else {
            map.put("body", JSON.toJSONString(result.getData()));
            map.put("url", JSON.toJSONString(result.getData()));
        }

        map.put("orderId", no);
        map.put("returnUrl", urlConfig.getReturnUrl());
        return new ModelAndView("wxpay", map);
    }

    @RequestMapping("/notify_ali")
    @ResponseBody
    public String async_al(@RequestBody String notifyData) {
        log.info("notify_ali notifyData:{}", JSON.toJSONString(notifyData));
        Result result = payService.asyncNotifyAli(notifyData);
        log.info("result notify_ali:{}", JSON.toJSONString(result));
        return "success";
    }

    @RequestMapping("/notify_wx")
    @ResponseBody
    public String notify_wx(@RequestBody String notifyData) {
        log.info("notify_wx notifyData:{}", JSON.toJSONString(notifyData));
        Result result = payService.asyncNotifyWx(notifyData);
        log.info("result notify_wx:{}", JSON.toJSONString(result));
        return "success";
    }

    @RequestMapping("/return")
    @ResponseBody
    public String returnUrl(HttpServletRequest request) {
        log.info("returnUrl notifyData:{}", JSON.toJSONString(request.getParameterMap()));
        return "returnUrl";
    }
}
